<template>
	<view class="container" v-if="show">
		<view class="content">
			<zeng-calen :actDay="actDay" :chooseDay="chooseDay" @onDayClick='onDayClick'
				@onMonth="onMonth"></zeng-calen>
		</view>
		<view class="list">
			<view class="title">送奶记录</view>
			<view class="list-box" v-if="dataList.length>0">
				<view class="item" v-for="(item,index) in dataList" :key="index">
					<view class="item-title display-flex">
						<view class="name">{{item.time}}</view>
						<view class="status" :class="item.status===2?'active':''">{{item.status===2?'已送达':'未送达'}}
						</view>
					</view>
					<view class="shop-box" v-for="(item1,index1) in item.itemList">
						<view class="shop-item display-flex">
							<view class="shop-name one-ellipsis">{{item1.itemName}}</view>
							<view class="">{{item1.itemCount}}个</view>
						</view>
					</view>
				</view>
			</view>
			<view class="empty" v-else>
				<image src="../../static/nodata.png" mode=""></image>
				<view>今日无送奶记录哦~</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive,
		watch,
		onMounted
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import utils from '/common/util.js'
	import dayjs from 'dayjs';
	const currDate = ref({
		month: dayjs().format('YYYY-MM')
	})
	//用户选择的日期
	const actDay = ref([])
	//已被投标的数据
	const chooseDay = ref([])
	const dataList = ref([])
	const show = ref(false)
	const getTestData = () => {
		chooseDay.value = ["2024-08-13", "2024-07-15", "2024-07-16", "2024-07-05"]
		show.value = true
		dataList.value = [{
			"time": "时间段：08:00、12:00等等",
			"status": 1, // "1 未送达，2 已送达",
			"itemList": [{
				"itemName": "酸奶",
				"itemCount": "几瓶"
			}]
		}, {
			"time": "时间段：08:00、12:00等等",
			"status": 2, //"1 未送达，2 已送达",
			"itemList": [{
				"itemName": "酸奶",
				"itemCount": "几瓶"
			}, {
				"itemName": "酸奶",
				"itemCount": "几瓶"
			}, {
				"itemName": "酸奶",
				"itemCount": "几瓶"
			}]
		}]
	}
	const getCalendar = () => {
		getApp().globalData.serviceApi({
			date: currDate.value.month
		}, 'calendar').then(res => {
			console.log('99999')
			const d = res.data ? res.data.days : []
			const arr = []
			for (let a = 0; a < d.length; a++) {
				arr.push(currDate.value.month + '-' + (d[a] < 10 ? '0' + d[a] : d[a]))
			}
			chooseDay.value = arr
			actDay.value = [arr[0]]
			getDayDetail()
		}).catch(error => {
			chooseDay.value = []
		})
		show.value = true
	}
	const getDayDetail = () => {
		getApp().globalData.serviceApi({
			date: actDay.value[0]
		}, 'dayDetail').then(res => {
			dataList.value = res.data ? res.data : []
		}).catch(error => {
			dataList.value = []
		})
	}
	const onDayClick = (data) => {
		let choose = data.date //用户点中的数据
		actDay.value = [choose]
		getDayDetail()

	}
	const onMonth = (data) => {
		currDate.value.month = data
		// getTestData()
		getCalendar()
		dataList.value = []
	}
	const getUserInfo = (count) => {
		if ((!userInfo.value || !userInfo.value.userId) && count > 0) {
			uni.showLoading({
				title: '请稍等...',
				mask: true
			})
			setTimeout(() => {
				if (getApp().globalData.userInfo && getApp().globalData.userInfo.userId) {
					userInfo.value = getApp().globalData.userInfo
					getCalendar()
				} else {
					getUserInfo(count--)
				}
			}, 300)
		} else {
			uni.hideLoading()
		}
	}
	// onMounted(() => {
	// 	console.log('******onMounted', getApp().globalData.userInfo, userInfo)
	// 	console.log('组件已经挂载')
	// 	getUserInfo(3)
	// })
	onMounted(() => {
		getCalendar()
		dataList.value = []
	})
</script>

<style lang="less" scoped>
	.container {
		width: 100%;
		min-height: 100%;
		padding: 60rpx 30rpx 30rpx;
		background-image: linear-gradient(#095ed3, #4a9bef);

		.list {
			font-size: 28rpx;
			background-color: #fff;
			padding-bottom: 10rpx;
			border-radius: 16rpx;
			margin-top: 20rpx;
			padding: 10rpx 25rpx 0;

			.title {
				height: 80rpx;
				line-height: 80rpx;
				font-size: 32rpx;
				background: url('/static/2003.png') left center / 40rpx no-repeat;
				padding-left: 50rpx;
			}

			.list-box {

				.item {
					font-size: 30rpx;
					padding: 30rpx 0;
					border-bottom: 1px solid #e5e5e5;

					.item-title {
						padding-bottom: 15rpx;

						.status {
							color: #999;
							font-weight: 600;

							&.active {
								color: #43a761
							}
						}
					}

					.shop-box {
						font-size: 26rpx;
						color: #999;
						line-height: 42rpx;

						.shop-name {
							width: calc(100% - 100rpx);
						}
					}

					&:first-child {
						padding-top: 10rpx;
					}

					&:last-child {
						border-bottom: 0;
					}
				}
			}
		}
	}

	.empty {
		font-size: 28rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		color: #666666;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-top: 60rpx;
		padding-bottom: 50rpx;

		image {
			width: 256rpx;
			height: 196rpx;
			margin-bottom: 40rpx;
		}
	}
</style>